/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2023-10-30 13:45:24
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2023-10-30 13:50:53
 * @FilePath: \projecte:\学习\react\my-react-project\src\utils\httpMethods.ts
 * @Description: 暴露出请求方法给api
 */
import request from "./requset";
import type { AxiosRequestConfig, AxiosResponse } from "axios";

type Data = {
    [index: string] : unknown
}
interface Http {
    get: (url: string, data?: Data, options?: AxiosRequestConfig) => Promise<AxiosResponse>
    post: (url: string, data?: Data, options?: AxiosRequestConfig) => Promise<AxiosResponse>
    put: (url: string, data?: Data, options?: AxiosRequestConfig) => Promise<AxiosResponse>
    deleted: (url: string, data?: Data, options?: AxiosRequestConfig) => Promise<AxiosResponse>
}

const httpMethods:Http = {
    get(url,data, options ) {
        return new Promise((reslove, reject) => {
            request.get(url, {params: data, ...options}).then(response => {
                reslove(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    post(url, data, options) {
        return new Promise((reslove, reject) => {
            request.post(url, data, {...options}).then(response => {
                reslove(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    put(url, data) {
        return new Promise((reslove, reject) => {
            request.put(url, data).then(response => {
                reslove(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    deleted(url, data, options)  {
        return new Promise((reslove, reject) => {
            request.delete(url, {data, ...options}).then(response => {
                reslove(response)
            }).catch(error => {
                reject(error)
            })
        })
    }
}

export default httpMethods